package com.cszjzx.member.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.dao.FieldDAOImpl;
import com.cszjzx.member.dao.TempleMemberDAO;
import com.cszjzx.member.dao.TempleMemberDAOImpl;
import com.cszjzx.member.po.Department;
import com.cszjzx.member.po.Field;
import com.cszjzx.member.po.Major;
import com.cszjzx.member.po.Master;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.po.Stage;
import com.cszjzx.member.service.DepartmentService;
import com.cszjzx.member.service.DepartmentServiceImpl;
import com.cszjzx.member.service.FieldServiceImpl;
import com.cszjzx.member.service.MajorService;
import com.cszjzx.member.service.MajorServiceImpl;
import com.cszjzx.member.service.MemberServiceImpl;
import com.cszjzx.member.service.TempleMemberService;
import com.cszjzx.member.service.TempleMemberServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.xml.internal.security.algorithms.implementations.IntegrityHmac;

@SuppressWarnings("serial")
public class ImportStudentInfoByHandAction extends ActionSupport {
	private int process;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private int checkcondition;
	private String name;
	private String idcard;
	private String department;
	private String major;
	private String message;
	private Member member;
	private int majorId;
	private int departmentId;
	private String departmentName;
	private String majorName;
	private String stageName;
	private List<Department> departments;
	private List<Major> majors;
	private TempleMemberService templeMemberService;
	private DepartmentService departmentService;
	private MajorService majorService;
	private String[][] info;
	private String details;				//用来存储操作的描述信息
	private int id;						//用来存储来自重新提交传过来的id值
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public int getMajorId() {
		return majorId;
	}

	public void setMajorId(int majorId) {
		this.majorId = majorId;
	}

	public int getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(int departmentId) {
		this.departmentId = departmentId;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public String getMajorName() {
		return majorName;
	}

	public void setMajorName(String majorName) {
		this.majorName = majorName;
	}

	public String getStageName() {
		return stageName;
	}

	public void setStageName(String stageName) {
		this.stageName = stageName;
	}

	public String[][] getInfo() {
		return info;
	}

	public void setInfo(String[][] info) {
		this.info = info;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public List<Major> getMajors() {
		return majors;
	}

	public void setMajors(List<Major> majors) {
		this.majors = majors;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public int getCheckcondition() {
		return checkcondition;
	}

	public void setCheckcondition(int checkcondition) {
		this.checkcondition = checkcondition;
	}

	public int getProcess() {
		return process;
	}

	public void setProcess(int process) {
		this.process = process;
	}

	// 显示手动添加界面（分为三个阶段的添加）
	public String show() throws Exception {
		departments = new DepartmentServiceImpl().getDepartments();
		Master master = (Master) request.getSession().getAttribute("master");
		int masterDepId = master.getDepartmentid();
		// 如果是管理员，则需要获得该系的所有班级列表
		if (masterDepId != 0) {
			MajorService majorService = new MajorServiceImpl();
			majors = majorService.getMajorsByDepartmentId(masterDepId);
		}
		// 跳转到showImportStudentInfoByHand.jsp
		return "showSuccess";
	}

	// 执行阶段添加操作
	@SuppressWarnings("unchecked")
	public String execute() {
		if (idcard == null || idcard.equals("")) {
			message = "所填信息不能为空";
			return "executeError";
		}
		if (name == null || name.equals("")) {
			message = "所填信息不能为空";
			return "executeError";
		}
		if (department == null || department.equals("")) {
			message = "所填信息不能为空";
			return "executeError";
		}
		if (major == null || major.equals("")) {
			message = "所填信息不能为空";
			return "executeError";
		}
		idcard = request.getParameter("idcard");
		templeMemberService = new TempleMemberServiceImpl();
		message = templeMemberService.checkBeforeImport(idcard, process);
		if(!message.equals("正确")){
			return "executeError";
		}
		String[] fieldsName;
		String[] fieldsValue;
		int size = 0;
		List<Field> fields = (List<Field>) request.getSession().getAttribute("fields");
		// size代表相应阶段所需要的字段信息
		for (int i = 0; i < fields.size(); i++) {
			if (fields.get(i).getStage() == process) {
				size++;
			}
		}
		fieldsName = new String[size]; // 字段的name
		fieldsValue = new String[size]; // 字段的value
		if (checkcondition == 0) {
			for (int i = 0, j = 0; i < fields.size() && j < size; i++) {
				if (fields.get(i).getStage() == process) {
					fieldsName[j] = fields.get(i).getName();
					String s = request.getParameter(fieldsName[j]);
					fieldsValue[j] = s;
					j++;
				}
			}
		} else {
			for (int i = 0, j = 0; i < fields.size() && j < size; i++) {
				if (fields.get(i).getStage() == process) {
					fieldsName[j] = fields.get(i).getName();
					String s = request.getParameter(fieldsName[j]);
					if (!fieldsName[j].equals("age")) {
						if (s == null || s.equals("")) {
							message = "所填信息不能为空";
							return "executeError";
						}
					}
					fieldsValue[j] = s;
					j++;
				}
			}
		}
		Master master = (Master) request.getSession().getAttribute("master");
		
		int operationType;
		if (process == 1 || process == -1) {
			operationType = 0;
		} else {
			operationType = 1;
		}
		boolean isSupper = master.isIssuper();
		boolean flag = templeMemberService.addTempleMember(fieldsName, fieldsValue, idcard, checkcondition,
				master.getId(), process, operationType, isSupper, id);
		if (!flag) {
			message = "身份证输入有误";
			return "executeError";
		}
		message = "记录添加成功";
		return "executeSuccess";
	}

	// 根据idcard显示修改团员信息页面，具体的信息全部展示
	public String showEdit() {
		MemberServiceImpl memberservice = new MemberServiceImpl();
		member = memberservice.getMemberByIdcard(idcard);
		departmentService = new DepartmentServiceImpl();
		majorService = new MajorServiceImpl();
		departments = departmentService.getDepartments();
		Master master = (Master) request.getSession().getAttribute("master");
		int masterDepId = master.getDepartmentid();
		MajorService majorService = new MajorServiceImpl();
		int memberDepartmentId = 0;
		for (int i = 0; i < departments.size(); i++) {
			if (member.getValue("department").equals(departments.get(i).getName())) {
				memberDepartmentId = departments.get(i).getId();
				break;
			}
		}
		majors = majorService.getMajorsByDepartmentId(memberDepartmentId);
		return "showEditSuccess";
	}

	// 修改团员信息的提交
	@SuppressWarnings("unchecked")
	public String edit() {
		MemberServiceImpl memberservice = new MemberServiceImpl();
		member = memberservice.getMemberByIdcard(idcard);
		process = Integer.parseInt(member.getValue("process"));
		String[] fieldsName;
		String[] fieldsValue;
		int size = 0;
		List<Field> fields = (List<Field>) request.getSession().getAttribute("fields");
		// size代表相应阶段所需要的字段信息
		for (int i = 0; i < fields.size(); i++) {
			if ((fields.get(i).getStage() <= process) && fields.get(i).getStage() > 0){
				size++;
			}
		}
		fieldsName = new String[size]; // 字段的name
		fieldsValue = new String[size]; // 字段的value
		// 为正式团员时
		if (process == 4) {
			size = fields.size();
			fieldsName = new String[size];
			fieldsValue = new String[size];
			for (int i = 0, j = 0; i < fields.size() && j < size; i++) {
				String s = request.getParameter(fieldsName[j]);
				// 不是‘备注’和'转出时间'字段
				if ((!(fields.get(i).getName().equals("remark"))) && (!(fields.get(i).getName().equals("outtime")))) {
					fieldsName[j] = fields.get(i).getName();
					if (s == null || s.equals("")) {
						message = "所填信息不能为空";
						return "editError";
					}
					if (fields.get(i).getDefaultValue() != null) {
						String ss = fields.get(i).getDefaultValue();
						if (!ss.equals(s)) {
							message = "所填信息不符合默认值要求";
							return "editError";
						}
					}
				}
				fieldsValue[j] = s;
				j++;
			}
		} else {
			for (int i = 0, j = 0; i < fields.size() && j < size; i++) {
				if ((fields.get(i).getStage() <= process) && fields.get(i).getStage() > 0) {
					fieldsName[j] = fields.get(i).getName();
					String s = request.getParameter(fieldsName[j]);
					//年龄是通过出生年龄直接计算的，不需要填写
					if ((s == null || s.equals("")) && (!fieldsName[j].equals("age"))) {
						message = "所填信息不能为空";
						return "editError";
					}
					fieldsValue[j] = s;
					j++;
				}
			}
		}
		Master master = (Master) request.getSession().getAttribute("master");
		templeMemberService = new TempleMemberServiceImpl();
		boolean flag,isSupper;
		isSupper = master.isIssuper();
		flag = templeMemberService.addUpdateTempleMember(fieldsName, fieldsValue, idcard, 1, 
												master.getId(), process, 1, isSupper, id);
		if (!flag) {
			message = "身份证填写错误";
			return "editError";
		}
		message = "记录修改成功";
		return "editSuccess";
	}

	public String delete() {
		Master master = (Master) request.getSession().getAttribute("master");
		boolean isSuper = master.isIssuper();
		templeMemberService = new TempleMemberServiceImpl();
		/*
		 * 如果是超级管理员的删除操作，是要根据idcard生成一条审核记录，
		 * 然后再将其删除，删除的话就必须根据id删除，但是此处无法获得id，所以必须要在一起删了
		 */
		// 超級管理员的删除操作（不需要审核）
		if (isSuper) {
			templeMemberService.addDeletedTempleMember(master.isIssuper(), idcard, master.getId(), 2);
		}
		// 管理员的删除操作（需要审核）
		else {
			templeMemberService.addDeletedTempleMember(master.isIssuper(), idcard, master.getId(), 1);

		}
		return "deleteSuccess";
	}
	
	/**
	 * 重新提交操作
	 * @return
	 */
	public String resubmit(){
		if(details.equals("信息修改")){
			member = new MemberServiceImpl().getMemberByIdcard(idcard);
			departmentName = member.getValue("department");
			majorName = member.getValue("major");
			majorService = new MajorServiceImpl();
			majors = majorService.getMajorsByDepartmentName(departmentName);
			Major major = new Major();
			for(int i=0;i<majors.size();i++){
				if(majors.get(i).getName().equals(majorName)){
					major = majors.get(i);
					break;
				}
			}
			majorId = major.getId();
			departmentId = major.getDepartmentId();
			List<Stage> stages = (List<Stage>)request.getSession().getAttribute("stages");
			for(int i=0;i<stages.size();i++){
				if(process == stages.get(i).getId()){
					stageName = stages.get(i).getName();
				}
			}
			return "resubmitEdit";
		}
		//“团员进阶”或者“新团员添加”都属于信息添加的内容，因此合在一起
		else{
			if(process == 4){
				process = 3;
			}
			return "resubmitAddMember";
		}
	}
}
